<?
if(!ANTIHACK) die("ANTIHACK...");
$getRound=$_GET["round"];
global $config,$baselink;
$year=$config["year"];
$idmember=$_GET["idmember"];
include_once(INCLUDES."paging.php");
if($_COOKIE['sort']=="")
	$_COOKIE['sort']="az";
if($_COOKIE['sort']=="az")
{
	$sort=" order by code asc";
	$smarty->assign("sort","az");
}
else
{
	$sort=" order by vote desc";
	$smarty->assign("sort","top");
}
$round = array();
$showRound = query("select * from round");
while($fetchRound = fetch($showRound))
{
	$round[] = array($fetchRound["roundname"],$fetchRound["code"],"",$year);
	if($getRound == $fetchRound["code"])
		$round[count($round)-1][2]=1;
}
$smarty->assign("round",$round);

$smarty->assign("year",$year);
if($idmember!="")
{
	$sm="memberID = ".$idmember." and ";
}	
$total=0;$start=0;$limit=9;
	$table = "baithi,round,member";
if($getRound=="")
{
	$where = "$sm baithi.memberID = member.id and round.id = baithi.round";
}
else
{
	$where = "$sm baithi.memberID = member.id and round.id = baithi.round and round.code = '$getRound'";
	$q=query("select * from round where code='$getRound'");
	$r=fetch($q);
}
if($_SESSION["admin"]!=1)
{
	$where="round.status!=2 and ".$where;
	if($r["status"]==2) {
		$smarty->assign("round_chuabd",1);
		}
}
$smarty->assign("roundname",$r["roundname"]);
	$_GET['code']="bai-thi";
	$listpage= paging($table,$where,$_GET,$total,$start,$limit,5);		
	$smarty->assign("listpage",$listpage);
	$q=query("select `baithi`.`id` as id, title,`baithi`.`code` as code,round,year,imgindex,vote,name from $table where $where  $sort limit $start,$limit");
$i=0;

while($f=fetch($q))
{
	if(($i+1)%3==0)
		$f["stt"]=0;
	else
		$f["stt"]=1;
	$f["imgindex"]=str_replace("./var/",$baselink."var/",$f["imgindex"]);
	$f["url"] = $baselink."xem-bai-thi/".xoadau($f['title'])."-".$f['id'].".html";
	$contest[] = $f;
	$i++;
}
$smarty->assign("total",$total);
$smarty->assign("contest",$contest);
$smarty->display("baithi.htm");
?>